class Solution(object):
    def countCornerRectangles(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        m = len(grid)
        n = len(grid[0])
        total = 0
        for i in range(m):
            for j in range(i + 1, m):
                count = 0
                for k in range(n):
                    if grid[i][k] == 1 and grid[j][k] == 1:
                        count += 1
                if count >= 2:
                    total += int(count * (count - 1) / 2)
        return total


data = Solution()
grid = [[1, 0, 0, 1, 0], [0, 0, 1, 0, 1], [0, 0, 0, 1, 0], [1, 0, 1, 0, 1]]
print(data.countCornerRectangles(grid))
grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
print(data.countCornerRectangles(grid))
grid = [[1, 1, 1, 1]]
print(data.countCornerRectangles(grid))
